#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
简单测试脚本：直接验证修复的关键功能
"""

import sys
import traceback
from note_parser import XiaohongshuNoteParser

def test_fixes():
    print("开始验证修复...")
    
    # 测试1: 验证_extract_comments方法的None检查
    print("\n测试1: 验证_extract_comments方法的None检查")
    try:
        parser = XiaohongshuNoteParser()
        
        # 模拟result字典
        result1 = {}
        # 手动设置comments为None
        result1['comments'] = None
        # 直接测试return语句的逻辑
        success = len(result1.get('comments', [])) > 0 if result1.get('comments') is not None else False
        print(f"  - 测试通过: 正确处理了None的情况，success={success}")
    except Exception as e:
        print(f"  - 测试失败: {e}")
        traceback.print_exc()
    
    # 测试2: 验证_extract_note_images方法的get()使用
    print("\n测试2: 验证_extract_note_images方法的get()使用")
    try:
        # 测试result.get('images', [])的安全性
        result2 = {}
        length = len(result2.get('images', []))
        print(f"  - 测试通过: get()方法安全工作，length={length}")
    except Exception as e:
        print(f"  - 测试失败: {e}")
        traceback.print_exc()
    
    # 测试3: 验证修复的健壮性
    print("\n测试3: 验证修复的健壮性")
    try:
        # 各种边缘情况测试
        test_cases = [
            {'comments': []},       # 空列表
            {'comments': None},     # None值
            {'comments': ["测试"]},  # 有内容的列表
            {},                     # 空字典
        ]
        
        for i, test_case in enumerate(test_cases):
            try:
                # 使用我们修复后的逻辑
                success = len(test_case.get('comments', [])) > 0 if test_case.get('comments') is not None else False
                print(f"  - 测试用例{i+1}通过: {test_case} -> success={success}")
            except Exception as e:
                print(f"  - 测试用例{i+1}失败: {e}")
    except Exception as e:
        print(f"  - 测试失败: {e}")
        traceback.print_exc()

if __name__ == "__main__":
    test_fixes()
